309C - Memory for Arrays - CodeForces Solution


binary search bitmasks greedy *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std; 
const long long maxSz = 1e6 + 5;

long long n, m;
long long mem[maxSz];
long long oddCnt[35];
long long ohmy[35];
long long orig[35];
long long powe[35];

void computePow() {
    powe[0] = 1;
    for(long long i = 1; i < 35; i++) {
        powe[i] = powe[i - 1] * 2;
    }
}

long long nums[maxSz];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    computePow();
    cin >> n >> m; 
    for(long long i = 0; i < n; i++) {
        cin >> mem[i]; 
    }
    for(long long i = 0; i < m; i++) {
        long long x; cin >> x;
        nums[i] = powe[x];
        ohmy[x]++;
        orig[x]++;
    }
    sort(nums, nums + m);

    for(long long i = 0; i < 35; i++) {
        long long cnt = 0;
        for(long long j = 0; j < n; j++) {
            cnt += mem[j] % 2 == 1;
            mem[j] /= 2;
        }
        oddCnt[i] = cnt;
    }

    long long i = 0; 
    long long ptr = 0;
    while(i < m && ptr < 35) {
        long long currCap = powe[ptr];
        long long s = 0; // maybe A[i] not sure 
        while(i < m && oddCnt[ptr] > 0 && nums[i] <= currCap) {
            // cout << "Bundle: ";
            while(i < m && s + nums[i] <= currCap) {
                s += nums[i]; 
                // cout << nums[i] << " ";
                i++;
            }
            // cout << '\n';
            oddCnt[ptr]--; 
            s = 0;
        }
        ptr++;
    }
    cout << i << '\n';



}


Comments

Submit
0 Comments
More Questions

1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings